<?php

namespace app\common\validate\tyyh;

class User extends Base
{
	/**
     * 验证规则
     */
    protected $rule = [
        'lock'                => 'require',
        'id'                  => 'require',
        'nickname'            => 'length:4,32',
        'username'            => 'length:4,32',
        'photo'               => 'max:100',
        'phone'               => 'require|mobile',
        'password'            => 'require|length:4,32',
        'newpassword'         => 'require|length:4,32',
        'newpassword2'        => 'require|newpassword2'
    ];


    /**
     * 提示消息
     */
    protected $message = [
        'lock.require'                  => '验证规则场景不存在',
        'id.require'                    => '用户ID不能为空',
        'id.isStore'                    => '请先填写店铺资料',
        'nickname.length'               => '用户昵称必须在4到32个字符之间',
        'username.require'              => '用户名称不能为空',
        'username.length'               => '用户名称必须在4到32个字符之间',
        'username.isSetUsername'        => '用户名已经设置',
        'username.isSetUsername,!'      => '请先设置用户名',
        'username.xunique'              => '用户名已存在',
        'photo.max'                     => '用户头像地址不能超过100个字符',
        'phone.require'                 => '手机号不能为空',
        'phone.mobile'                  => '手机号不合法',
        'password.require'              => '密码不能为空',
        'password.length'               => '密码必须在4到32个字符之间',
        'password.password'             => '密码不正确',
        'password.isSetPassword'        => '密码不正确',
        'password.password4username'    => '用户名或密码错误',
        'newpassword.require'           => '新密码不能为空',
        'newpassword.length'            => '新密码必须在4到32个字符之间',
        'newpassword2.require'          => '再次输入新密码不能为空',
        'newpassword2.newpassword2'     => '两次输入的密码不一致'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'update'            => ['nickname', 'username', 'photo'],
        'set_password'      => ['username' => 'require|length:4,32', 'password' => 'require|length:4,32|password4username', 'newpassword', 'newpassword2'],
        'username'          => ['id'],
        'set_user_password' => ['id' => 'require|isStore', 'username' => 'require|length:4,32|isSetUsername|xunique:tyyh\\User', 'password']
    ];

    //请先填写店铺资料
    protected function isStore($value, $rule, $data = []) {
        $examine = loadEnum('StoreExamine');
        $store = loadOnly('tysj\\Store')->current_store();
        return $examine->isEdit($store['examine']);
    }

    //验证密码是否正确
    protected function password($value, $rule, $data = []) {
        $passwordTool = loadTool('Password');
        $row = loadOnly('Model')->find('tyyh\\User', $data['id']);
        if(empty($row)) {
            return false;
        }
        return $passwordTool->encryption($value, $row['salt']) == $row['password'];
    }

    //根据用户名验证密码是否正确
    protected function password4username($value, $rule, $data = []) {
        $passwordTool = loadTool('Password');
        $row = loadOnly('tyyh\\User')->find4username($data['username']);
        if(empty($row)) {
            return false;
        }
        return $passwordTool->encryption($value, $row['salt']) == $row['password'];
    }

    //验证密码是否未设置或正确
    protected function isSetPassword($value, $rule, $data = []) {
        $row = loadOnly('Model')->find('tyyh\\User', $data['id']);
        if($row['password'] == '' && $row['salt'] == '' && $value == '') {
            return true;
        }
        return $this->password($value, $rule, $data);
    }

    //验证两次密码输入是否一致
    protected function newpassword2($value, $rule, $data = []) {
        return $value == $data['newpassword'];
    }

    protected function require($value, $rule, $data = [], $name = '') {
        if($rule) {
            $arr = explode(',', $rule);
            $fun = $arr[0];
            if(method_exists($this, $fun)) {
                $result = $this->$fun($value, $rule, $data);
                if(isset($arr[1]) && $arr[1] == '!') {
                    $result = !$result;
                }
                if($result) {
                    return true;
                } else {
                    return $this->message[$name . '.' . $rule];
                }
            }
        }
        return parent::require($value, $rule, $data);
    }

    protected function isSetUsername($value, $rule, $data = []) {
        $row = loadOnly('Model')->find('tyyh\\User', $data['id']);
        if(empty($row)) {
            return false;
        }
        return $row['username'] == '';
    }


}
